/**
 * 邮箱
 * @param {*} s
 */
export function isEmail(s) {
  return /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/.test(
    s
  );
}

/**
 * 手机号码
 * @param {*} s
 */
export function isMobile(s) {
  return /^1[0-9]{10}$/.test(s);
}

/**
 * 电话号码
 * @param {*} s
 */
export function isPhone(s) {
  return /^([0-9]{3,4}-)?[0-9]{7,8}$/.test(s);
}

/**
 * URL地址
 * @param {*} s
 */
export function isURL(s) {
  return /[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?/.test(
    s
  );
}
/**
 * 固定电话
 * @param {*} s
 */
export function isFixedPhone(s) {
  return /^0\d{2,3}-?\d{7,8}$/.test(s);
}
/**
 * 数组
 * @param {*} s
 */
export function isArray(obj) {
  return Object.prototype.toString.call(obj) === "[object Array]";
}
// 获取上个月
export function getPreMonth(date) {
  var arr = date.split("-");
  var year = arr[0]; //获取当前日期的年份
  var month = arr[1]; //获取当前日期的月份
  var day = arr[2]; //获取当前日期的日
  var days = new Date(year, month, 0);
  days = days.getDate(); //获取当前日期中月的天数
  var year2 = year;
  var month2 = parseInt(month) - 1;
  if (month2 == 0) {
    year2 = parseInt(year2) - 1;
    month2 = 12;
  }
  var day2 = day;
  var days2 = new Date(year2, month2, 0);
  days2 = days2.getDate();
  if (day2 > days2) {
    day2 = days2;
  }
  if (month2 < 10) {
    month2 = "0" + month2;
  }
  var t2 = year2 + "-" + month2 + "-" + day2;
  return t2;
}
// 获取上上个月
export function getPrePreMonth(date) {
  var arr = date.split("-");
  var year = arr[0]; //获取当前日期的年份
  var month = arr[1]; //获取当前日期的月份
  var day = arr[2]; //获取当前日期的日
  var days = new Date(year, month, 0);
  days = days.getDate(); //获取当前日期中月的天数
  var year2 = year;
  var month2 = parseInt(month) - 2;
  if (month2 == 0) {
    year2 = parseInt(year2) - 2;
    month2 = 12;
  }
  var day2 = day;
  var days2 = new Date(year2, month2, 0);
  days2 = days2.getDate();
  if (day2 > days2) {
    day2 = days2;
  }
  if (month2 < 10) {
    month2 = "0" + month2;
  }
  var t2 = year2 + "-" + month2 + "-" + day2;
  return t2;
}
// 获取下个月
export function getNextMonth(date) {
  var arr = date.split("-");
  var year = arr[0]; //获取当前日期的年份
  var month = arr[1]; //获取当前日期的月份
  var day = arr[2]; //获取当前日期的日
  var days = new Date(year, month, 0);
  days = days.getDate(); //获取当前日期中的月的天数
  var year2 = year;
  var month2 = parseInt(month) + 1;
  if (month2 == 13) {
    year2 = parseInt(year2) + 1;
    month2 = 1;
  }
  var day2 = day;
  var days2 = new Date(year2, month2, 0);
  days2 = days2.getDate();
  if (day2 > days2) {
    day2 = days2;
  }
  if (month2 < 10) {
    month2 = "0" + month2;
  }

  var t2 = year2 + "-" + month2 + "-" + day2;
  return t2;
}
/**
 * 计算两个日期有效天数
 * param=>date1:小日期   date2:大日期
 */
export function getBetweenDays(date1, date2) {
  //
  var sdate = new Date(date1);

  var now = new Date(date2);

  var days = now.getTime() - sdate.getTime();

  var day = parseInt(days / (1000 * 60 * 60 * 24));

  return day;
}
/**
 * 获取时间并格式化时间
 * 调用：date=dateFormat("YYYY-mm-dd HH:MM:SS", date)
 * 当前时间 this.dateFormat(new Date())
 */
export function dateFormat(time) {
  var date = new Date(time);

  var year = date.getFullYear();

  /* 在日期格式中，月份是从0开始的，因此要加0

* 使用三元表达式在小于10的前面加0，以达到格式统一 如 09:11:05

* */

  var month =
    date.getMonth() + 1 < 10
      ? "0" + (date.getMonth() + 1)
      : date.getMonth() + 1;

  var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();

  var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();

  var minutes =
    date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();

  var seconds =
    date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();

  // 拼接

  return (
    year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds
  );
}
